<?php
namespace app\admin\controller;

use app\admin\logic\GoodsLogic;
use app\admin\model\Banner;
use app\BaseController;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class Base extends BaseController{
    //adv 首页轮播
     public function adv(){
         $list=Banner::bannerList();
         View::assign('list',$list);
          return View::fetch();
     }
     //adv  首页轮播添加
     public function advAdd(){
          if (Request::isPost()) {
              $post = Request::post();
              list($status, $paths) = Upload::imgUpload('photo');
              if ($status) {
                  $post['photo'] = $paths;
              }
              $post['addtime'] = time();
              $res = Banner::bannerAdd($post);
              if ($res) {
                  $this->success('添加成功', url('adv'));
              } else {
                  $this->error('添加失败');
              }
          } else {

              return View::fetch();
          }
      }
     //adv首页轮播修改
     public function advEdit(){
            if (Request::isPost()){
                $param=Request::param();
                list($status, $photo) = Upload::imgUpload('photo');
                if (!$status){
                    $this->error($photo);
                }
                if ($status && !empty($photo)) {
                    list($path_date, $path_dir)=GoodsLogic::setImgUrl($photo,'banner/');
                    $param['photo']=$path_dir;
                }
                $obj=Banner::find($param['id']);
                $old= $obj->photo;
                $res=Banner::bannerEdit($param);
                if ($res){
                    //单图移动临时目录到正式目录
                    if ($status && !empty($photo)) {
                        GoodsLogic::setImgDir($photo, $path_date, $path_dir, './images/');
                    }
                    //删除单图老图路径
                    if ($status && !empty($photo) &&!empty($old)){unlink('./images/'.$old);}
                    $this->success('修改成功',url('adv'));
                }else{
                    $this->error('修改失败');
                }
            }else{
                $param=Request::param();
                $find=Banner::bannerEdit($param);
                View::assign($find);
                return View::fetch();
            }
          }
     //adv首页轮播删除
     public function advDel(){
        $get=Request::get();
        $res=Banner::bannerDel($get);
        if ($res){
            $this->success('删除成功',url('adv'));
        }else{
            $this->error('删除失败');
        }
      }
        //info网站设置
    public function info(){


        return View::fetch();
    }
        //pass修改密码
    public function pass(){
         $find=Session::get('user');
         if (Request::isPost()){
             $post=Request::post();
             if ($find['upwd']!=md5($post['mpass'])){
                 $this->error('原密码不正确');
             }
             $res= \app\admin\model\Users::where('id','=',$find['id'])->update(['upwd'=>md5($post['newpass'])]);
             if ($res){
                 $user = \app\admin\model\Users::find($find['id']);
                 Session::set('user', $user);
                 $this->success('修改成功');
             }else{
                 $this->success('修改失败');
             }
         }else{
             View::assign($find->toArray());
             return View::fetch();
         }

    }

}